package com.yanqu.road.server.cmd.activity.climbtower;

import com.yanqu.road.entity.activity.climbtower.ClimbTowerConfig;
import com.yanqu.road.entity.activity.climbtower.ClimbTowerRoleInfo;
import com.yanqu.road.entity.activity.climbtower.UserClimbTowerData;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ClimbTowerProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.climbtower.ClimbTowerModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.climbtower.ClimbTowerMgr;
import com.yanqu.road.server.pb.PlayerPb;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;


@Cmd(code = Protocol.S_CLIMB_TOWER_TOP_CLIMB, desc = "新幻境爬塔")
public class ClimbTowerTopClimbCmd extends AbstractCommand {

    private ClimbTowerProto.ClimbTowerTopClimbReqMsg reqMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = ClimbTowerProto.ClimbTowerTopClimbReqMsg.parseFrom(packet.getBytes());
        if (reqMsg.getCellNum() <= 0) {
            return false;
        }
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        // 没有活动
        if (!ActivityMgr.activityInTime(ClimbTowerMgr.getActivityInfo()) || ClimbTowerMgr.getConfig() == null) {
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }

        ClimbTowerModule module = player.getModule(ClimbTowerModule.class);
        UserClimbTowerData userData = module.getUserData();
        if (userData == null) {
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }

        if (!module.isInTopCell()) {
            ret = GameErrorCode.E_CLIMB_TOWER_LOCK;
            return;
        }
        int pic = 0;
        int lv = 0;

        ClimbTowerConfig config = ClimbTowerMgr.getConfig();
        if (null != config) {
            ClimbTowerRoleInfo roleInfo = config.getClimbTowerRoleInfo(userData.getExp());
            if (null != roleInfo) {
                pic = roleInfo.getPic();
                lv = roleInfo.getLevel();
            }
        }

        int activityId = ClimbTowerMgr.getActivityInfo().getActivityId();
        player.sendPacket(CrossProtocol.C_CLIMB_TOWER_TOP_CLIMB,
                ClimbTowerProto.ClimbTowerCrossTopClimbReqMsg.newBuilder().
                        setActivityId(activityId).setAbility(userData.getAbility().toString()).setPic(pic).setLv(lv).
                        setMaxCellNum(userData.getMaxTopCell()).setCellNum(reqMsg.getCellNum()).setTargetUserId(reqMsg.getTargetUserId()).
                        setUserBaseInfo(PlayerPb.parsePlayerBaseTempMsg(player.getUserInfo())));
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            ClimbTowerProto.ClimbTowerTopClimbRespMsg.Builder respMsg = ClimbTowerProto.ClimbTowerTopClimbRespMsg.newBuilder();
            respMsg.setRet(ret);
            respMsg.setOldTopCell(0);
            respMsg.setMaxTopCell(0);
            respMsg.setNewTopCell(0);
            player.sendPacket(Protocol.U_CLIMB_TOWER_TOP_CLIMB, respMsg);
        }
    }
}
